package com.vict.util;

import cn.hutool.core.io.FileUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 */
@Slf4j
public class FileUtils {

    public static List<File> searchAllUsbDrive() {
        File[] allDrive = File.listRoots();
        List<File> usbDrive = new ArrayList();
        for(File drive : allDrive){
            if(drive.getAbsolutePath().toLowerCase().startsWith("C".toLowerCase(Locale.ROOT))){
                continue;
            }
            // if(drive.getAbsolutePath().toLowerCase().startsWith("D".toLowerCase(Locale.ROOT))){
            //     continue;
            // }

            if(drive.canRead() && drive.canWrite()){
                usbDrive.add(drive);
            }
        }
        if(usbDrive == null || usbDrive.size() == 0){
            System.out.println("未找到可用U盘");
        }
        return usbDrive;
    }

    public static List<File> getAllSubFile(File file){
        File[] ls = FileUtil.ls(file.getAbsolutePath());
        List<File> files = new ArrayList<>();
        for(File subFile : Optional.ofNullable(ls).orElse(new File[0])){
            if(subFile.isHidden()){
                continue;
            }
            files.add(subFile);
        }
        return files;
    }

    /** 获取文件夹下所有文件及文件夹 */
    public static List<File> getAllSubFileOrderByCreateTime(File file){
        File[] ls = FileUtil.ls(file.getAbsolutePath());
        List<File> files = new ArrayList<>();
        for(File subFile : Optional.ofNullable(ls).orElse(new File[0])){
            if(subFile.isHidden()){
                continue;
            }
            files.add(subFile);
        }

        files.sort((o1, o2) -> {
            try {
                BasicFileAttributes attr1 = Files.readAttributes(o1.toPath(), BasicFileAttributes.class);
                long l1 = attr1.creationTime().toMillis();

                BasicFileAttributes attr2 = Files.readAttributes(o2.toPath(), BasicFileAttributes.class);
                long l2 = attr2.creationTime().toMillis();

                if(l1 < l2){
                    return -1;
                }else if(l1 > l2){
                    return 1;
                }else{
                    return 0;
                }
            } catch (IOException e) {
                log.error("", e);
            }
            return 0;
        });

        return files;
    }

    /** 获取文件夹下所有文件及文件夹 */
    public static List<File> getAllSubFileOrderByCreateTime(File file, String fileType){
        File[] ls = FileUtil.ls(file.getAbsolutePath());
        List<File> files = new ArrayList<>();
        for(File subFile : Optional.ofNullable(ls).orElse(new File[0])){
            if(subFile.isHidden()){
                continue;
            }
            if(!subFile.isFile()){
                continue;
            }
            if(!subFile.getName().toLowerCase().endsWith(fileType.toLowerCase())){
                continue;
            }
            files.add(subFile);
        }

        files.sort((o1, o2) -> {
            try {
                BasicFileAttributes attr1 = Files.readAttributes(o1.toPath(), BasicFileAttributes.class);
                long l1 = attr1.creationTime().toMillis();

                BasicFileAttributes attr2 = Files.readAttributes(o2.toPath(), BasicFileAttributes.class);
                long l2 = attr2.creationTime().toMillis();

                if(l1 < l2){
                    return -1;
                }else if(l1 > l2){
                    return 1;
                }else{
                    return 0;
                }
            } catch (IOException e) {
                log.error("", e);
            }
            return 0;
        });

        return files;
    }

    /**
     * 修改文件创建时间, 更新时间
     */
    @SneakyThrows
    public static void updateFileTime(File file, Long time){
        BasicFileAttributeView basicFileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
        basicFileAttributeView.setTimes(FileTime.from(time, TimeUnit.MILLISECONDS), FileTime.from(time, TimeUnit.MILLISECONDS), FileTime.from(time, TimeUnit.MILLISECONDS));
    }

    /** 重命名 */
    public static File rename(File file, String name){
        File file1 = new File(file.getParentFile(), name);
        file.renameTo(file1);
        return file1;
    }

    public static void move(File file, File file2){
        FileUtil.move(file, file2, true);
    }

    public static void main(String[] args) {
        // File file = new File("E:\\temp");
        // FileUtil.mkdir("E:\\temp");
        FileUtil.move(new File("E:\\00001.刀郎 - 十送红军 (慢四版).mp3"), new File("E:\\temp\\00001.刀郎 - 十送红军 (慢四版).mp3")
                , true);
    }
}
